package com.aric.springbootmusic.service.impl;

import com.aric.springbootmusic.entity.dto.AddRoleDTO;
import com.aric.springbootmusic.entity.pojo.Role;
import com.aric.springbootmusic.entity.vo.FindAllRoleVO;
import com.aric.springbootmusic.entity.vo.ShowAllRoleVO;
import com.aric.springbootmusic.exceptions.NameDuplicationException;
import com.aric.springbootmusic.mapper.RoleMapper;
import com.aric.springbootmusic.service.RoleService;
import com.aric.springbootmusic.utils.Result;
import com.aric.springbootmusic.utils.ResultUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Transactional(rollbackFor = Exception.class) //开启事务
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public Result addNewRole(AddRoleDTO roleDTO) throws NameDuplicationException {
        //重名验证
        LambdaQueryWrapper<Role> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getRoleName,roleDTO.getRoleName());

        Long count = roleMapper.selectCount(queryWrapper);
        if(count>0){
            throw new NameDuplicationException("已经存在相同的角色了");
        }

        Role role = Role.builder()
                        .roleName(roleDTO.getRoleName())
                        .roleDesc(roleDTO.getRoleDesc())
                        .isDelete(roleDTO.getIsDelete())
                        .build();

        int i = roleMapper.insert(role);
        if(i>0){
            Role queryRole = roleMapper.selectById(role.getRoleId());
            return ResultUtil.getResponseResult(HttpStatus.OK.value(), "角色添加成功!",queryRole);
        }
        return ResultUtil.getResponseResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), "角色添加失败!",null);
    }

    @Override
    public Result findAllRole(Integer currentPage,Integer pageSize) {
        Page<Role> p=new Page(currentPage,pageSize);
        Page<Role> rolePage = roleMapper.selectPage(p, null);
        long total = rolePage.getTotal();
        long current = rolePage.getCurrent();
        long size = rolePage.getSize();
        List<Role> roles = rolePage.getRecords();

        if(Objects.nonNull(roles)){
            List<FindAllRoleVO> roleVo=new ArrayList<>();
            roles.forEach(role -> {
                FindAllRoleVO roleVO = FindAllRoleVO.builder()
                        .id(role.getRoleId())
                        .roleName(role.getRoleName())
                        .roleDesc(role.getRoleDesc())
                        .build();
                roleVo.add(roleVO);
            });

            ShowAllRoleVO roleVO = ShowAllRoleVO.builder().roles(roleVo).total(total).pageSize(size).currentPage(current).build();
            return ResultUtil.getResponseResult(HttpStatus.OK.value(), "查询角色成功!",roleVO);
        }

        return ResultUtil.getResponseResult(HttpStatus.INTERNAL_SERVER_ERROR.value(),"没有数据",null);
    }

    @Override
    public Result findRoleById(Integer id) {
        Role role = roleMapper.selectById(id);
        if(Objects.nonNull(role)){
            FindAllRoleVO singleRole = FindAllRoleVO.builder().id(role.getRoleId())
                    .roleName(role.getRoleName())
                    .roleDesc(role.getRoleDesc())
                    .build();
            return ResultUtil.getResponseResult(HttpStatus.OK.value(), "查询成功!",singleRole);

        }
        return ResultUtil.getResponseResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), "查询失败!",null);
    }

    @Override
    public Result updateRoleById(Role role) {
        int i = roleMapper.updateById(role);
        if(i>0){
            Role roleView = roleMapper.selectById(role.getRoleId());
            FindAllRoleVO roleVO = FindAllRoleVO.builder()
                    .id(roleView.getRoleId())
                    .roleName(roleView.getRoleName())
                    .roleDesc(roleView.getRoleDesc())
                    .build();

            return ResultUtil.getResponseResult(HttpStatus.OK.value(), "更新成功!",roleVO);
        }
        return ResultUtil.getResponseResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), "更新失败!",null);
    }

    @Override
    public Result deleteRole(Integer id) {
        int i = roleMapper.deleteById(id);
        if(i>0){
            return ResultUtil.getResponseResult(HttpStatus.OK.value(), "删除成功!",null);
        }
        return ResultUtil.getResponseResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), "删除失败!",null);
    }

    @Override
    public Result findRoles() {
        List<Role> roles = roleMapper.selectList(null);
        if(Objects.nonNull(roles)){
            List<FindAllRoleVO> roleList=new ArrayList<>();
            roles.forEach(role -> {
                FindAllRoleVO roleVO = FindAllRoleVO.builder()
                        .id(role.getRoleId())
                        .roleName(role.getRoleName())
                        .roleDesc(role.getRoleDesc())
                        .build();
                roleList.add(roleVO);
            });
            return ResultUtil.getResponseResult(HttpStatus.OK.value(), "获取角色成功!",roleList);
        }
        return ResultUtil.getResponseResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), "获取角色失败!",null);
    }

    @Override
    public Result updateRoleAuthor(Integer roleId, List<Integer> psIds) {
        //删除
        Integer deleteFlag=roleMapper.deletePsById(roleId);
        //新增
        Integer addFlag=roleMapper.addPs(roleId,psIds);
        //返回一个权限数据，暂时不做
        if(deleteFlag>0 && addFlag>0){
            return ResultUtil.getResponseResult(HttpStatus.OK.value(), "更新成功！",null);
        }

        return ResultUtil.getResponseResult(HttpStatus.OK.value(), "更新失败！",null);
    }
}
